/**
 * 配置时间段管理初始化
 */
var DeviceTime = {
    id: "DeviceTimeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
DeviceTime.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: '名称', field: 'name', visible: true, align: 'center', valign: 'middle'},
            {title: '所属门禁', field: 'device', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle',formatter:function(value,row,index){
            var a="";
            if(row.status==1){
            var a = '<span class="value">' + "允许通过" + '</span>';
             return a;
            }
            else{
            var a = '<span class="value">' + "禁止通过" + '</span>';
            return a;
            }
            }},
            {title: '星期', field: 'week', visible: true, align: 'center', valign: 'middle'/*,formatter:function(value,row,index){
            var a="";
            if(row.week==1){
            var a = '<span class="value">' + "星期一" + '</span>';
             return a;
            }
            else if(row.week==2){
                        var a = '<span class="value">' + "星期二" + '</span>';
                         return a;
                        }
            else if(row.week==3){
                        var a = '<span class="value">' + "星期三" + '</span>';
                         return a;
                        }
            else if(row.week==4){
                        var a = '<span class="value">' + "星期四" + '</span>';
                         return a;
                        }
            else if(row.week==5){
                        var a = '<span class="value">' + "星期五" + '</span>';
                         return a;
                        }
            else if(row.week==6){
                        var a = '<span class="value">' + "星期六" + '</span>';
                         return a;
                        }
            else{
            var a = '<span class="value">' + "星期日" + '</span>';
            return a;
            }

            }*/},
            {title: '起始时间', field: 'begin', visible: true, align: 'center', valign: 'middle'},
            {title: '结束时间', field: 'end', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
DeviceTime.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        DeviceTime.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加配置时间段
 */
DeviceTime.openAddDeviceTime = function () {
    var index = layer.open({
        type: 2,
        title: '添加配置时间段',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/deviceTime/deviceTime_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看配置时间段详情
 */
DeviceTime.openDeviceTimeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '配置时间段详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/deviceTime/deviceTime_update/' + DeviceTime.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除配置时间段
 */
DeviceTime.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/deviceTime/delete", function (data) {
            Feng.success("删除成功!");
            DeviceTime.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("deviceTimeId",this.seItem.id);
        ajax.start();
    }
};

DeviceTime.reset = function () {
    var index = layer.open({
        type: 2,
        title: '重置时间段',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/deviceTime/deviceTime_reset'
    });
    this.layerIndex = index;
};

/**
 * 查询配置时间段列表
 */
DeviceTime.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['device'] = $("#device").val();
    queryData['week'] = $("#week").val();
    DeviceTime.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = DeviceTime.initColumn();
    var table = new BSTable(DeviceTime.id, "/deviceTime/list", defaultColunms);
    table.setPaginationType("client");
    DeviceTime.table = table.init();
});
